package com.irdstudio.apicenter.console.controller.authenticate;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.irdstudio.apicenter.console.dm.AppWxModelConfig;
import com.irdstudio.apicenter.console.service.authenticate.AppWxModelConfigService;



@Controller
@RequestMapping(value = "/appWxModelConfig")
public class AppWxModelConfigAction {
	
	@Autowired
	private AppWxModelConfigService appWxModelConfigService;
	

	// 插入跳转
	@RequestMapping(value = "jumpAddWxModelConfig")
	public String jumpAddWxMenuConfig(HttpServletRequest request) {
	    String appid = request.getParameter("appid");
		request.setAttribute("appid", appid);
		return "auth/addWxModelConfig";
	}

	// 插入保存
	@RequestMapping(value = "addWxModelConfig")
	public void addWxMenuConfig(@ModelAttribute("appWxModelConfig") AppWxModelConfig appWxModelConfig,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		flag = appWxModelConfigService.insertOne(appWxModelConfig);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	// 更新跳转
	@RequestMapping(value = "jumpUpdateWxModelConfig")
	public String jumpUpdateWxMenuConfig(HttpServletRequest request)
			throws SQLException, ClassNotFoundException {
		String dxModelId = request.getParameter("dxModelId");
		int modelLineNum = Integer.parseInt(request.getParameter("modelLineNum"));
		String appid = request.getParameter("appid");
		AppWxModelConfig appWxMenuConfig = appWxModelConfigService.queryAppTradePrivate(dxModelId, modelLineNum);
		//将换行符转义
		if(appWxMenuConfig.getLineChangePart()!=null){
			appWxMenuConfig.setLineChangePart(appWxMenuConfig.getLineChangePart().replaceAll("\n", "<br>"));
		}
		
		request.setAttribute("appWxModelConfig", appWxMenuConfig);
		request.setAttribute("appid", appid);
		return "auth/updateWxModelConfig";
	}

	// 更新保存
	@RequestMapping(value = "updateWxModelConfig")
	public void updateWxMenuConfig(@ModelAttribute("appWxModelConfig") AppWxModelConfig appWxModelConfig,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		flag = appWxModelConfigService.update(appWxModelConfig);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	// 删除
	@RequestMapping(value = "deleteWxModelConfig")
	public void deleteWxMenuConfig(HttpServletRequest request,
			HttpServletResponse response) throws SQLException,
			ClassNotFoundException {
		int flag = -1;
		String dxModelId = request.getParameter("dxModelId");
		int modelLineNum = Integer.parseInt(request.getParameter("modelLineNum"));
		flag = appWxModelConfigService.delete(dxModelId, modelLineNum);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	// 查看
	@RequestMapping(value = "queryWxModelConfigDetail")
	public String queryWxMenuConfigDetail(HttpServletRequest request) throws SQLException,
			ClassNotFoundException {
		String dxModelId = request.getParameter("dxModelId");
		int modelLineNum = Integer.parseInt(request.getParameter("modelLineNum"));
		String appid = request.getParameter("appid");
		AppWxModelConfig appWxModelConfig = appWxModelConfigService.queryAppTradePrivate(dxModelId, modelLineNum);
		request.setAttribute("appWxModelConfig", appWxModelConfig);
		request.setAttribute("appid", appid);
		return "auth/queryWxModelConfigDetail";
	}


	
	
	
	
}
